package com.yzt.redpack.dialog;

import static android.app.Activity.RESULT_OK;
import static com.luck.picture.lib.utils.ToastUtils.showToast;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.yzt.redpack.GameTransHostActivity;
import com.yzt.plugin_widget.RoundImageView;
import com.yzt.redpack.R;
import com.yzt.zhuju.dialog.BottomBaseDialog;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.mine.wallet.WalletBean;
import com.yzt.zhuju.widget.tuiview.TUITextView;
import com.yzt.zhuju.widget.tuiview.TUIView;
import com.yzt.zhuju.widget.widget.ImageLoadManger;

/**
 * 作者：bobwang
 * 创建时间：2023-10-12 21:27:43
 * 描述：发布红包
 */
@SuppressLint("SetTextI18n")
public class GameRedPkgDialog extends BottomBaseDialog {

    private TUITextView tvLuck;
    private TUITextView tvExclusive;
    private TUIView viewLuck;
    private TUIView viewExclusive;
    private EditText et_count;
    private EditText et_total;
    private EditText et_msg;
    private TextView tv_msg_count;

    private RoundImageView img_avatar;
    private EditText et_exclusive_count;

    private LinearLayout ll_wallet_container;

    private int userCount;

    private String extraId;

    private String targetUserId;

    private WalletBean walletEntry;

    private int extra_scenario_type;

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_add_red_pkg;
    }

    @Override
    public void bindView(View v) {
        userCount = getArguments().getInt("extra_user_count", 0);
        extraId = getArguments().getString("extra_game_id");
        extra_scenario_type = getArguments().getInt("extra_scenario_type", 0);
        tvLuck = findViewById(R.id.tv_luck);
        tvExclusive = findViewById(R.id.tv_exclusive);
        viewLuck = findViewById(R.id.view_luck);
        viewExclusive = findViewById(R.id.view_exclusive);
        et_count = findViewById(R.id.et_count);
        et_total = findViewById(R.id.et_total);

        img_avatar = findViewById(R.id.img_avatar);
        et_exclusive_count = findViewById(R.id.et_exclusive_count);

        ll_wallet_container = findViewById(R.id.ll_wallet_container);

        et_msg = findViewById(R.id.et_msg);
        tv_msg_count = findViewById(R.id.tv_msg_count);
        TextView tv_user_count = findViewById(R.id.tv_user_count);
        tv_user_count.setText("共" + userCount + "人");
        tvLuck.setSelected(true);
        viewLuck.setSelected(true);
        initClick();
        getCoin();
    }

    /**
     * 监听处理
     */
    private void initClick() {

        LinearLayout ll_luck_container = findViewById(R.id.ll_luck_container);
        LinearLayout ll_exclusive_container = findViewById(R.id.ll_exclusive_container);

        findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAllowingStateLoss();
            }
        });

        tvLuck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvLuck.setSelected(true);
                viewLuck.setSelected(true);
                tvExclusive.setSelected(false);
                viewExclusive.setSelected(false);
                ll_luck_container.setVisibility(View.VISIBLE);
                ll_exclusive_container.setVisibility(View.GONE);
            }
        });

        tvExclusive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvLuck.setSelected(false);
                viewLuck.setSelected(false);
                tvExclusive.setSelected(true);
                viewExclusive.setSelected(true);
                ll_luck_container.setVisibility(View.GONE);
                ll_exclusive_container.setVisibility(View.VISIBLE);
            }
        });

        // 红包数
        et_count.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String content = et_count.getText().toString();
                if (!content.equals("")) {
                    int count = Integer.parseInt(content);
                    if (count > userCount) {
                        et_count.setText(String.valueOf(userCount));
                    }
                }
            }
        });

        // 发出的糖币数量
        et_total.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String content = et_total.getText().toString();
                if (!content.equals("")) {
                    int count = (int) walletEntry.getCandyNum();
                    if (count < Integer.parseInt(content)) {
                        ll_wallet_container.setVisibility(View.VISIBLE);
                    }
                }
            }
        });

        // 专属糖币数量
        et_exclusive_count.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String content = et_exclusive_count.getText().toString();
                if (!content.equals("")) {
                    int count = (int) walletEntry.getCandyNum();
                    if (count < Integer.parseInt(content)) {
                        ll_wallet_container.setVisibility(View.VISIBLE);
                    }
                }
            }
        });

        et_msg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String content = et_msg.getText().toString();
                tv_msg_count.setText(content.length() + "/16");
            }
        });

        // 选择发送对象
        findViewById(R.id.ll_select_user).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), GameTransHostActivity.class);
                intent.putExtra("extra_type", extra_scenario_type);
                intent.putExtra("extra_id", extraId);
                startActivityForResult(intent, 2);
            }
        });

        // 充值
        findViewById(R.id.tv_recharge).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        //发布
        findViewById(R.id.tv_post).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String count = et_count.getText().toString();
                String total = et_total.getText().toString();
                String msg = et_msg.getText().toString();
                String exclusiveCount = et_exclusive_count.getText().toString();

                if (tvLuck.isSelected()) {
                    if (count.equals("")) {
                        showToast(getContext(), "请输入红包数量");
                        return;
                    }

                    if (total.equals("")) {
                        showToast(getContext(), "请输入糖币数量");
                        return;
                    }
                } else {
                    if (targetUserId == null) {
                        showToast(getContext(), "请选择发送对象");
                        return;
                    }

                    if (exclusiveCount.equals("")) {
                        showToast(getContext(), "请输入糖币数量");
                        return;
                    }
                }

//                if (msg.equals("")) {
//                    showToast(getContext(), "请输入留言信息");
//                    return;
//                }

                postRed();
            }
        });
    }

    /**
     * 获取的糖币数量
     */
    private void getCoin() {
        BizLayer.getInstance().getMineModule().wallet(new AresSubscriber<WalletBean>() {
            @Override
            protected void onSuccess(WalletBean wallet) {
                walletEntry = wallet;
            }
        });
    }

    /**
     * 发红包
     */
    private void postRed() {

        String count = et_count.getText().toString();
        String total = et_total.getText().toString();
        String exclusiveCount = et_exclusive_count.getText().toString();
        String msg = et_msg.getText().toString();

        JSONObject json = new JSONObject();
        json.put("scenarioType", extra_scenario_type);
        if (tvLuck.isSelected()) {
            json.put("redPacketNum", count);
            json.put("candyCoin", total);
        } else {
            json.put("targetUserId", targetUserId);
            json.put("candyCoin", exclusiveCount);
        }
        if (extra_scenario_type == 2) {
            json.put("activityId", extraId);
        } else {
            json.put("chatGroupId", extraId);
        }
        if (msg.equals("")){
            json.put("dsc", "大吉大利");
        }else{
            json.put("dsc", msg);
        }
        json.put("redType", tvLuck.isSelected());

        BizLayer.getInstance().getDynamicModule().addRedPkg(json, new AresSubscriber<Object>() {
            @Override
            protected void onSuccess(Object bean) {
                showToast(getContext(), "红包发送成功");
                dismissAllowingStateLoss();
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2 && resultCode == RESULT_OK) {
            targetUserId = data.getStringExtra("extra_data");
            String avatar = data.getStringExtra("extra_avatar");
            ImageLoadManger.loadIv(getContext(), avatar, img_avatar);
        }
    }

}
